package xyz.eden.boot.core.tool;

import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    private static final char SEPARATOR = '_';


    /**
     * 转换到驼峰式命名
     * <p>case: user_info -> </p>
     *
     * @param name
     * @return
     */
    public static String convertToCamelCase(String name) {
        // 快速检查
        if (org.apache.commons.lang3.StringUtils.isBlank(name)) {
            return "";
        }
        // 不含下划线，仅将首字母大写
        if (!name.contains("_")) {
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }

        String[] segmentArr = name.split("_");
        return Arrays.stream(Optional.of(segmentArr).orElse(ArrayUtils.EMPTY_STRING_ARRAY))
                .map(segment -> {
                    return segment.substring(0, 1).toUpperCase() + segment.substring(1);
                }).collect(Collectors.joining());
    }

    /**
     * 转驼峰变量
     * 例如：user_name->userName
     */
    public static String toCamelCase(String s) {



        if (s == null) {
            return null;
        }
        if (s.indexOf(SEPARATOR) == -1) {
            return s;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 截取字符串
     * <p>
     * Examples:
     * <blockquote>
     * <pre>
     *     substring("hello_word", 1) returns "ello_world"
     *     substring("hello_word", -1) returns "d"
     * </pre>
     * </blockquote>
     * </p>
     *
     * @param str
     * @param start
     * @return
     */
    public static String substring(final String str, int start) {
        return substring(str, start, length(str));
    }

    /**
     * 截取字符串
     * <p>
     * Examples:
     * <blockquote>
     * <pre>
     *     substring("hello_word", 1, 2) returns "e"
     *     substring("hello_word", 1, -2) returns "ello_wor"
     * </pre>
     * </blockquote>
     * </p>
     *
     * @param str
     * @param startIndex
     * @param endIndex
     * @return
     */
    public static String substring(final String str, int startIndex, int endIndex) {
        // 快速检测
        if (str == null) {
            return EMPTY;
        }
        // 允许循环截取
        int strLen = length(str);
        startIndex = startIndex < 0 ? strLen + startIndex : startIndex;
        endIndex = endIndex < 0 ? strLen + endIndex : endIndex;

        // 不合法索引
        if (strLen < startIndex || startIndex > endIndex) {
            return EMPTY;
        }
        return str.substring(startIndex, endIndex);
    }
}
